// JavaScript Document
$(document).ready(function() {
  var module = $("#module").val();
	
  //Function to check all checkboxes
  $("#checkAll").click(function(){
    var checked_status = this.checked;
    $("input[@name=checkBoxList]").each(function() {
      this.checked = checked_status;
    });
  });

  $('#edit-poll-search-active').datepicker({
    dateFormat:'mm-dd-yy'
  });
  $('#edit-poll-search-expire').datepicker({
    dateFormat:'mm-dd-yy'
  });
  //$('#edit-sitecombo').dropdownchecklist();
  /*$("#edit-apply").click(function()	{
    filterservice();
    return false;
  });*/
});



function applyAction(pageno, action, searchid){

  //prepare the field values for filtering
  var search_sitename    = $.trim($("#edit-sitename").val());
  var search_servicename = $("#edit-servicename").val();
  var search_serviceurl  = $("#edit-serviceurl").val();
  var search_verb        = $("#edit-verb").val();
  var search_action      = $("#edit-action").val();
  var search_sorting      = $("#hid_sort").val();
  var siteid = $('#edit-sitecombo').val();
		
  var data = "";
  if(search_sitename!==""){
    data += "search_sitename="+search_sitename;
  }
  if(search_servicename!==""){
    if(data!="")
      data += "&";
    data += "search_servicename="+search_servicename;
  }
  if(search_serviceurl!==""){
    if(data!="")
      data += "&";
    data += "search_serviceurl="+search_serviceurl;
  }
  if(search_verb!==""){
    if(data!="")
      data += "&";
    data += "search_verb="+search_verb;
  }
  if(search_action!==""){
    if(data!="")
      data += "&";
    data += "search_action="+search_action;
  }
  if(search_sorting!==""){
    if(data!="")
      data += "&";
    data += "sorting="+search_sorting;
  }
  if(siteid!==""){
    if(data!="")
      data += "&";
    data += "search_siteid="+siteid;
  }

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").html("");
  $("#pagingdivbottom").html("");
  $.ajax({
    type : "POST",
    url : baseurl+"service/filter",
    data : data+(pageno>=1?'&page_no='+pageno:''),

    cache : false,
    success : function (msg) {

      $("#listingResultset").html(msg);
			 
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno);
      var counter = $("#hid_totalRec").val();
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyAction('+(page-1)+',\''+action+'\',\''+searchid+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += ' <a href="javascript:void(0)" onclick = "applyAction('+(page+1)+',\''+action+'\')" class="red"> | Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
      }
		
    }
  });

}

function changesiteInfo(){
  var siteid = $('#edit-sitecombo').val();
  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").html("");
  $("#pagingdivbottom").html("");

  $.ajax({
    type : "POST",
    url : baseurl+"service/filter",
    data : "search_siteid="+siteid+"&page_no=1",

    cache : false,
    success : function (msg) {
      $("#listingResultset").html(msg);
      var pageno = 1;
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno);
      var counter = $("#hid_totalRec").val();
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyAction('+(page-1)+',\'Previous\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += ' <a href="javascript:void(0)" onclick = "applyAction('+(page+1)+',\'Next\')" class="red"> | Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
      }

    }
  });
}


function closeDialog(testId){
  $('#dialog').dialog('destroy');
  makeArchived(testId);
}

function openViewDetail(id, status, sitename){
  openDialog(viewservicedialog, [id, status, sitename], 500, 250, 'View Service Details');
}

function viewservicedialog(id){
  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type : "GET",
    url : baseurl+'serviceregistry/details/'+id[0]+'/'+id[1]+'/'+(id[2]?'/'+id[2]:''),
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $("#edit-service-id").val(id);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function createServiceRegisty(id){		
  reopen_dialog(450,160,"Create Service Site Registry");
  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type : "GET",
    url : baseurl+'serviceregistry/create_service/'+id,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function addService(){
  var pageno                =  $("#hid_pageno").val();
  var service_name          =  $('#edit-serviceregistry-name').val();
  var service_url           =  $('#edit-serviceregistry-url').val();
  var service_verb          =  $('#edit-serviceregistry-verb').val();
  var service_action        =  $('#edit-serviceregistry-action').val();
  var id                    =  $("#edit-service-id").val();
  var siteId                =  $('#edit-sitecombo').val();

  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');

  $.ajax({
    type: "POST",
    url: baseurl + 'serviceregistry/update/'+id+'/'+siteId,
    data: '&service_name='+ encodeURIComponent(service_name) + '&service_url=' + encodeURIComponent(service_url) + '&service_verb=' + encodeURIComponent(service_verb) + '&service_action=' + encodeURIComponent(service_action),
    success: function(message){
      $('#dialog').dialog('destroy');
      if(message == "Success"){
        applyAction(pageno,$('#edit-apply').attr('actionitem'),"general");
        openAlertDialog(300,150,"Created", "New Service is Updated","success");
      }
      else{
        openAlertDialog(300,150,"Error", "This Service not Updated");
      }

    }
  });
}

function edit_serviceregistry(id,status){
  var siteid=$('#edit-sitecombo').val();
  if(status == 1){
    createServiceRegisty(id);
    return false;
  }else{
    reopen_dialog(450,220,"Edit Service Site Registry");
    editservicedialog(id, siteid);
    return false;
  }
}


function editservicedialog(id, siteid){
  $.ajax({
    type : "GET",
    url : baseurl+'serviceregistry/edit/details/'+id+'/'+siteid,
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function update_service_registry(){
  var pageno                =  $("#hid_pageno").val();
  var service_name          =  $('#edit-serviceregistry-name').val();
  var service_url           =  $('#edit-serviceregistry-url').val();
  var service_verb          =  $('#edit-serviceregistry-verb').val();
  var service_action        =  $('#edit-serviceregistry-action').val();
  var id                    =  $("#edit-service-id").val();
  var siteId                =  $('#edit-sitecombo').val();
  //var typelist = new Array();
  //typelist = ['GET', 'POST','DELETE','PUT'];

  if(service_url == ''){
    $('#createservice_error').html('<font color=red>Please Fill Service URL</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }
 
  if((service_verb == '')){
    $('#createservice_error').html('<font color=red>Please Fill Verb</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }

  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type: "POST",
    url: baseurl + 'serviceregistry/save/'+id+'/'+siteId,
    data: '&service_name='+ service_name + '&service_url=' + service_url + '&service_verb=' + service_verb + '&service_action=' + service_action,
    success: function(message){
      if(message == "Success"){
        $('#dialog').dialog('destroy');
        applyAction(pageno,$('#edit-apply').attr('actionitem'),"general");
        //reopen_dialog(500,250,"View Service Site Registry");
        openAlertDialog(300,150,"Updated", "Service is Updated", "success");
      }
      else{
        openAlertDialog(300,150,"Error", "This Service not Updated");
      }
    }
  });
  return false;
}



function delete_service(id){
  var pageno = $("#hid_pageno").val();
  openConfirmBox(function(){
    $('#alertdialog').dialog('destroy');
    $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
    $.ajax({
      type: "POST",
      url: baseurl + 'serviceregistry/delete/'+id,
      success: function(message){
        if(message == "Success"){
          $('#dialog').dialog('destroy');
          openAlertDialog(300,150,"Deleted", "Service deleted successfully", "success");
          applyAction(pageno, $('#edit-apply').attr('actionitem'), "general");
        }
        else{
          $('#dialog').dialog('destroy');
          openAlertDialog(300,125,"Error", "Servicename could not deleted");
        }
      }
    });
  },300,150,"Confirm Delete","Do you want to delete this service?");
	
  return false;
}


function save_service(){	
  var pageno = $("#hid_pageno").val();
  //var sitename            = $('#edit-create-sitename').val();
  var servicename         = $('#edit-create-servicename').val();
  var serviceurl          = $('#edit-create-serviceurl').val();
  var verb                = $('#edit-create-verb').val();
  var action              = $('#edit-create-action').val();
  var type                = $('#edit-create-type').val();
   
  var tylelist = new Array();
  typelist = ['GET', 'POST','DELETE','PUT'];

  if((servicename == '')){
    $('#createservice_error').html('<font color=red>Please Fill Servicename</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }
  if((serviceurl == '')){
    $('#createservice_error').html('<font color=red>Please Fill Service URL</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }

  if((verb == '')){
    $('#createservice_error').html('<font color=red>Please Fill Verb</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }


  if((action == '')){
    $('#createservice_error').html('<font color=red>Please Fill Action</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }

  if((type == 'ANY')){
    $('#createservice_error').html('<font color=red>Please Choose type</font>');
    $("#createservice_error").show();
    $('#dialog').scrollTop(0);
    return false;
  }

  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type: "POST",
    url: baseurl + 'serviceregistry/update',
    data: '&servicename=' + servicename+ '&serviceurl=' + serviceurl + '&verb=' + verb+ '&action=' + action+'&type=' + type,
    success: function(message){
      $('#dialog').html(message);
      // id = message;
      $('#dialog').dialog('destroy');
      openAlertDialog(300,200,"Updated", "New Services Updated");
      applyAction(pageno,$('#edit-apply').attr('actionitem'),"general");
    }
  });
  return false;

}



function filterdata_serviceregistry(){
  //prepare the field values for filtering
  var search_servicename = $.trim($("#edit-servicename").val());
  var search_serviceurl = $("#edit-serviceurl").val();
  var search_verb = $("#edit-verb").val();
  var search_action = $("#edit-action").val();
    
  var data = "";
  if($.trim(search_servicename)!=""){
    data+="&search_servicename="+search_servicename;
  }
  if($.trim(search_serviceurl)!=""){
    data+="&search_serviceurl="+search_serviceurl;
  }
  if($.trim(search_verb)!="" && search_verb != "ANY"){
    data+="&search_verb="+search_verb;
  }
  if($.trim(search_action)!=""){
    data+="&search_action="+search_action;
  }

  return data;
}